<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号码" prop="idno">
        <el-input
          v-model="queryParams.idno"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['user:qj:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['user:qj:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['user:qj:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['user:qj:export']"
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table
      v-loading="loading"
      :data="qjList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column v-if="false" label="id" align="center" prop="id" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证号码" align="center" prop="idno" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gender" :value="scope.row.gender" />
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" prop="mobile" />
      <el-table-column label="户籍地" align="center" prop="hjd" />
      <el-table-column label="管控地所在区县" align="center" prop="areaName" />
      <el-table-column label="决定机关" align="center" prop="jdjg" />
      <el-table-column
        label="戒毒期限起日期"
        align="center"
        prop="jdqxqrq"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jdqxqrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入所时间" align="center" prop="rsrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rsrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出所时间" align="center" prop="csrq" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.csrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['user:qj:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['user:qj:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改强戒人员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号码" prop="idno">
          <el-input v-model="form.idno" placeholder="请输入身份证号码" />
        </el-form-item>
        <el-form-item prop="gender" label="性别">
          <el-select
            v-model="form.gender"
            placeholder="请选择性别"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.gender"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="户籍地" prop="hjd">
          <el-input v-model="form.hjd" placeholder="请输入户籍地" />
        </el-form-item>
        <el-form-item label="负责人" prop="fzr">
          <user-member-select v-model="form.fzr"/>
        </el-form-item>
        <el-form-item label="管控地所在区县" prop="area">
          <area-cascader v-model="form.area" />
        </el-form-item>
        <el-form-item label="决定机关" prop="jdjg">
          <el-input v-model="form.jdjg" placeholder="请输入决定机关" />
        </el-form-item>
        <el-form-item label="戒毒期限起日期" prop="jdqxqrq">
          <el-date-picker
            v-model="form.jdqxqrq"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择戒毒期限起日期"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="入所时间" prop="rsrq">
          <el-date-picker
            v-model="form.rsrq"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入所时间"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="出所时间" prop="csrq">
          <el-date-picker
            v-model="form.csrq"
            clearable
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出所时间"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/user/userInfo";

export default {
  name: "Qj",
  dicts: [
    'gender',
  ],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 强戒人员表格数据
      qjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        subType: 'drug_qj',
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        idno: undefined,
        age: undefined,
        gender: undefined,
        mobile: undefined,
        hjd: undefined,
        area: undefined,
        jdjg: undefined,
        jdqxqrq: undefined,
        rssj: undefined,
        cssj: undefined,
      },
      // 表单参数
      form: {
        type: '2'
      },
      // 表单校验
      rules: {
        id: [{ required: true, message: "id不能为空", trigger: "blur" }],
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        idno: [
          { required: true, message: "身份证号码不能为空", trigger: "blur" },
        ],
        mobile: [{ required: true, message: "手机号不能为空", trigger: "blur" }, {
          pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
          message: "请输入正确的手机号码",
          trigger: "blur"
        }],
        gender: [{ required: true, message: "性别不能为空", trigger: "blur" }],
        hjd: [{ required: true, message: "户籍地不能为空", trigger: "blur" }],
        area: [
          {
            required: true,
            message: "管控地所在区县不能为空",
            trigger: "blur",
          },
        ],
        jdjg: [
          { required: true, message: "决定机关不能为空", trigger: "blur" },
        ],
        fzr: [
          { required: true, message: "负责人不能为空", trigger: "blur" },
        ],
        jdqxqrq: [
          {
            required: true,
            message: "戒毒期限起日期不能为空",
            trigger: "blur",
          },
        ],
        rssj: [
          { required: true, message: "入所时间不能为空", trigger: "blur" },
        ],
        cssj: [
          { required: true, message: "出所时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询强戒人员列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then((response) => {
        this.qjList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        type: '2',
        id: undefined,
        name: undefined,
        idno: undefined,
        gender: undefined,
        mobile: undefined,
        hjd: undefined,
        fzr: undefined,
        area: undefined,
        jdjg: undefined,
        jdqxqrq: undefined,
        rsrq: undefined,
        csrq: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加强戒人员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const qjId = row.id || this.ids;
      getUser(qjId).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改强戒人员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateUser(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addUser(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const qjIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除强戒人员编号为"' + qjIds + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delUser(qjIds);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "user/qj/export",
        {
          ...this.queryParams,
        },
        `qj_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
